#include <stdio.h>

int main(int argc, char const *argv[]) {
  const char baseDigits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  int convertedNumber[64];
  long int numberToConvert;
  int nextDigit,base,index = 0;

  //获取数字和基数
  printf("Number to be converted? ");
  scanf("%ld",&numberToConvert);
    printf("Base? ");
  do {
    scanf("%i",&base );
    if (base<2||base>16) {
      printf("wrong input!try again!");
    }
  } while(base<2||base>16);

  //转换为指定的基数
  do {
    convertedNumber[index] = numberToConvert%base;
    ++index;
    numberToConvert = numberToConvert/base;
  } while(numberToConvert!=0);

  //以倒序显示结果
  printf("Converted number = ");
  for (--index; index >= 0; --index) {
    nextDigit = convertedNumber[index];
    printf("%c", baseDigits[nextDigit]);
  }
  printf("\n");

  return 0;
}
